<?php
/**
 * 整形校验器
 *
 * @package util.validator.impl
 * @since 0.2
 */
class IIntegerValidator extends IAbstractValidator {
	protected $min = null;
	protected $max = null; 
	
	const NOT_INTEGER = "not_integer";
	const TOO_SHORT = "too_short";
	const TOO_LARGE = "too_large";
	
	/**
	 * 构造器
	 *
	 * @since 0.2
	 */
	public function __construct() {
		$this->addToFactory();
	}
	
	/**
	 * 执行校验
	 *
	 * @param mixed $value 应用校验的值
	 * @return boolean
	 * @since 0.2
	 */
	protected function validate($value) {
		if (!preg_match("/^\\d+$/", $value)) {
			$this->addMessage(self::NOT_INTEGER, "the input is not integer");
			return false;
		}
		if (!is_null($this->min) && $value < $this->min) {
			$this->addMessage(self::TOO_SHORT, "the number is too short");
			return false;
		}
		if (!is_null($this->max) && $value > $this->max) {
			$this->addMessage(self::TOO_LARGE, "the number is too large");
			return false;
		}
		return true;
	}
}

?>